﻿using Basic.FormRemoteModel;
using WeDonekRpc.Helper;

namespace Basic.FormModel.RecordItem
{
    public class RecordItemDto
    {
        public long TableId { get; set; }

        public string ColName { get; set; }

        public bool IsNull { get; set; }

        public int RowIndex { get; set; }
        /// <summary>
        /// 控件类型
        /// </summary>
        public ControlType ColType { get; set; }

        public FormRecordDataType ItemType { get; set; }

        public string StrVal { get; set; }

        public decimal? NumVal { get; set; }

        public long? LongVal { get; set; }

        public bool? BoolVal { get; set; }

        public object GetValue ()
        {
            if ( this.IsNull )
            {
                return null;
            }
            if ( this.ItemType == FormRecordDataType.String )
            {
                return this.StrVal;
            }
            else if ( this.ItemType == FormRecordDataType.Number )
            {
                return this.NumVal.Value;
            }
            else if ( this.ItemType == FormRecordDataType.DateTime || this.ItemType == FormRecordDataType.Date )
            {
                DateTime time = this.LongVal.Value.ToDateTime();
                return time;
            }
            else if ( this.ItemType == FormRecordDataType.Time )
            {
                long val = this.LongVal.Value;
                return string.Format("{0}:{1}", ( val / 60 ).ToString().PadLeft(2, '0'), ( val % 60 ).ToString().PadLeft(2, '0'));
            }
            else if ( this.ItemType == FormRecordDataType.Array && this.StrVal.IsNotNull() )
            {
                return this.StrVal.Substring(1, this.StrVal.Length - 2).Split('|');
            }
            else if ( this.ItemType == FormRecordDataType.Long )
            {
                return this.LongVal.Value.ToString();
            }
            else if ( this.ItemType == FormRecordDataType.Bool )
            {
                return this.BoolVal.Value;
            }
            return null;
        }
    }
}
